package csc.fresher.security;

import java.io.IOException;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import csc.fresher.bankservice.BankService;
import csc.fresher.dao.HistoryDAO;
import csc.fresher.dao.UserInfoDAO;
import csc.fresher.domain.History;
import csc.fresher.domain.UserInfo;

/**
 * After logout. Update time end to table history.
 * 
 *
 */
@Component
public class CustomLogoutHandler extends SimpleUrlLogoutSuccessHandler {
	
	@Autowired
	private BankService services;

	@Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response,
            Authentication authentication) throws IOException, ServletException {

		int id = CustomLoginSuccessHandler.idHistory;
		History history = services.getHistory(id);
		history.setEnd(new Date());
		services.saveOrUpdate(history);
        
        super.onLogoutSuccess(request, response, authentication);       
    }
	
		

}
